//index.js
var utils = require('../../utils/util.js');
//获取应用实例
const app = getApp();

var pageIndex = 1,
    filterIndex = 1,
    condition = false,
    is_slide = true;
Page({
  data: {
      scroll_h:50,
      list_data:[],
      is_downloading:null,
      type:[],
      slide_is_hide:true
  },
    reset_but:function(e){
        console.log(e);
        var type = this.data.type
        for(var i=0;i<type.length;i++){
            type[i].checked = false;
        }

        is_slide = true;
        pageIndex = 1;
        filterIndex = 1;
        this.setData({
            type:type
        })
    },
    slide_ce:function(){    //显示隐藏
        /* console.log(111);*/
        if(this.data.slide_is_hide == true){
            var bl = false;
        }else{
            var bl = true;
        }

        this.setData({
            slide_is_hide:bl
        });
    },
    formSubmit:function(e){   //提交
        console.log(e.detail.value);
        var param = e.detail.value;
        param.pageNo=filterIndex;
        var that =this;
        console.log(param);
        wx.request({
            url: 'https://www.ggd4.cn/wap/projectUpload/getData',
            data: param,
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function(res) {
                console.log(res.data);
                condition = param;
                utils.submit_cg(that,res)
            }
        });
        console.log('form发生了submit事件，携带数据为：', e.detail.value);
    },
    get_type: function (e) {    //阶段依赖
        this.setData({
            type: utils.radio('type',this,e)
        })
    },
    searchScrollLower:function(){
        console.log('到底了');
        wx.showLoading({
            title: '加载中....',
            mask:true
        });
        var that = this;
        pageIndex++;
        console.log(pageIndex);
        wx.request({
            url: 'https://www.ggd4.cn/wap/ProjectUpload/getData',
            data: {
                pageNo:pageIndex
            },
            header: {
                'Content-Type': 'application/json;charset=UTF-8;'
            },
            method:'GET',
            success: function(res) {
                /*that*/
                wx.hideLoading();
                console.log(res.data.data.length<=0);
                /*console.log(res.data.data);
                console.log(that.data.list_data);
                console.log(that.data.list_data.concat(res.data.data) );*/
                if(!res.data.data.length<=0){
                    that.setData({
                        list_data:that.data.list_data.concat(res.data.data)     //累加数据总量
                    })
                }else{
                    wx.showToast({
                        title: '没有更多了',
                        icon: 'none',
                        duration: 2000
                    })
                }

            }
        });
    },
    download:function(e){
      var that = this;
      var status = that.data.is_downloading;
      console.log(e.currentTarget.dataset.url);
      if(status!=null){
          wx.showToast({
              title:'只能同时下载一个',
              icon:'none'
          });
          return;
      }else{
          that.setData({
              is_downloading : e.currentTarget.dataset.index
          });
          wx.showToast({
              title:'开始下载..',
              icon:'none'
          });
      }

        wx.downloadFile({
            url: e.currentTarget.dataset.url, //仅为示例，并非真实的资源
            success: function(res) {
                // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
               /* console.log(res);*/
                console.log(res);
                if (res.statusCode === 200) {
                    console.log(res.tempFilePath);
                    var ls_path = res.tempFilePath;
                    that.setData({
                        is_downloading:null
                    });
                    wx.showModal({
                        title: '下载成功',
                        content: '是否保存',
                        success: function(res) {
                            wx.saveFile({
                                tempFilePath: ls_path,
                                success: function(res) {
                                    var savedFilePath = res.savedFilePath;
                                    console.log(savedFilePath);
                                    wx.showModal({
                                        title: '已保存到本地',
                                        content: savedFilePath,
                                        success: function(res) {
                                            if (res.confirm) {
                                                console.log('用户点击确定')
                                            } else if (res.cancel) {
                                                console.log('用户点击取消')
                                            }
                                        }
                                    })
                                },
                                fail:function(res){
                                    console.log(res);
                                    wx.setClipboardData({
                                        data: e.currentTarget.dataset.url,
                                        success: function(res) {
                                            wx.showModal({
                                                title: '文件过大:已复制链接请在浏览器打开',
                                                content: e.currentTarget.dataset.url,
                                                success: function(res) {
                                                    if (res.confirm) {
                                                        console.log('用户点击确定')
                                                    } else if (res.cancel) {
                                                        console.log('用户点击取消')
                                                    }
                                                }
                                            })
                                        }
                                    })
                                }
                            })
                        }
                    })

                }
            },
            fail:function(res){
                that.setData({
                    is_downloading:null
                });
                wx.setClipboardData({
                    data: e.currentTarget.dataset.url,
                    success: function(res) {
                        wx.showModal({
                            title: '文件过大:已复制链接请在浏览器打开',
                            content: e.currentTarget.dataset.url,
                            success: function(res) {
                                if (res.confirm) {
                                    console.log('用户点击确定')
                                } else if (res.cancel) {
                                    console.log('用户点击取消')
                                }
                            }
                        })
                    }
                })
            }
        })
    },
  onLoad: function () {
      var that = this;
      utils.init_h(that);
      wx.request({
          url: 'https://www.ggd4.cn/wap/ProjectUpload/getData',
          data: {
              pageNo:pageIndex
          },
          header: {
              'Content-Type': 'application/json;charset=UTF-8;'
          },
          method:'GET',
          success: function(res) {
              /*that*/
           /*   console.log(res);*/
              that.setData({
                  list_data:res.data.data,
                  slide_is_hide:true
              })
          }
      })
      wx.request({
          url: 'https://www.ggd4.cn/wap/projectUpload/index',
          header: {
              'Content-Type': 'application/json;charset=UTF-8;'
          },
          method:'GET',
          success: function(res) {
              /*that*/
              console.log(res);
              that.setData({
                  type:res.data.data
              })
          }
      })
  },
    onReady:function(){
      utils.onready(this);
    }
  // ,
  // getUserInfo: function(e) {
  //   console.log(e)
  //   app.globalData.userInfo = e.detail.userInfo
  //   this.setData({
  //     userInfo: e.detail.userInfo,
  //     hasUserInfo: true
  //   })
  // }
})
